﻿using DBModels;
using Method.DAL;
using Method.DateTime;
using Models;
using Models.VModels;
using RedisHelper.Article;
using RedisHelper.Notify;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace Api.WDS.Methods
{
    public class LatestMet
    {
        /// <summary>
        /// 获取用户最新消息集合
        /// </summary>
        /// <param name="uid"></param>
        /// <returns></returns>
        public V_NotifyCount GetUserAllNotify(int uid)
        {
            return new V_NotifyCount()
            {
                LatestLikesCount = new LatestLike().GetLatestLikeCount(uid),
                LatestCommentsCount = new LatestPay().GetLatestPayCount(uid),
                LatestMessageCount = new LatestMessage().GetLatestMessageCount(uid)
            };
        }

        /// <summary>
        /// 获取用户最新点赞集合
        /// </summary>
        /// <param name="uid"></param>
        /// <returns></returns>
        public List<V_LatestLike> GetUserLatestLike(int uid)
        {
            List<V_LatestLike> list = new List<V_LatestLike>();
            var dataR = new LatestLike().GetLike(uid).OrderByDescending(u => Convert.ToInt64(u.LikeId)).ToList();
            using (ArticeContext context = new ArticeContext())
            {
                dataR.ForEach(u =>
                {
                    //获取用户信息
                    var info = new GetUserInfoByUid().GetUserInfo(u.UserId);
                    if (info != null)
                    {
                        var curInfo = new V_LatestLike()
                        {
                            LikeId = GetPastTime.getTime(u.LikeId),
                            UserId = u.UserId,
                            Avatar = StaticConfigPath.AvatarPath(info.Avatar),
                            NickName = info.NickName,
                            IsClassification = u.IsClassification,
                            ArticleId = u.ArticleId,
                            CommentId = u.CommentId
                        };
                        //如果是文章
                        if (curInfo.IsClassification is 0)
                        {
                            //获取文章标题
                            var art = context.ArticleLists.FirstOrDefault(u => u.Id == curInfo.ArticleId);
                            curInfo.Title = art != null ? art.Title : "该文章已被删除";
                        }
                        //如果是评论
                        if (curInfo.IsClassification is 1)
                        {
                            //获取评论内容
                            var com = new Comment().GetCommentList(curInfo.ArticleId).FirstOrDefault(u => u.CommentId == curInfo.CommentId);
                            curInfo.Context = com != null ? com.Text : "该评论已被删除";
                        }
                        list.Add(curInfo);
                    }
                });
            }
            return list;
        }

        /// <summary>
        /// 清空最新点赞
        /// </summary>
        /// <param name="token"></param>
        /// <returns></returns>
        public bool DelLatestLike(int uid)
        {
            return new LatestLike().DelLike(uid);
        }

        /// <summary>
        /// 清空最新评论
        /// </summary>
        /// <param name="token"></param>
        /// <returns></returns>
        public bool DelLatestComment(int uid)
        {
            return new LatestPay().DelLatestPay(uid);
        }

        /// <summary>
        /// 清空最新消息
        /// </summary>
        /// <param name="token"></param>
        /// <returns></returns>
        public bool DelLatestMessage(int uid)
        {
            return new LatestMessage().DelLatestMessage(uid);
        }
    }
}
